﻿package {
	import com.utils.HtmlText;
	import com.utils.Image;
	import com.utils.URI;
	import com.views.interfaces.ITip;
	
	import flash.display.*;
	import flash.events.*;
	import flash.text.*;

	public class Buff extends Sprite {

		private var _onDoubleRemoveBuff:Function;
		private var _tip:ITip;
		private var _allBuffData:Object;
		private var _allBuffList:Array;
		private var _buffMc:MovieClip;
		private var _isShow:Boolean = true;

		public function Buff() {
			this._onDoubleRemoveBuff = new Function();
			super();
			this._allBuffData = {};
			this._allBuffList = [];
		}

		public function set addBuff(_arg1:Object):void {
			this.contrastBuff(_arg1);
		}

		public function set removeBuff(_arg1:String):void {
			this.clearBuff(_arg1);
		}

		public function set onDoubleRemoveBuff(_arg1:Function):void {
			this._onDoubleRemoveBuff = _arg1;
		}

		public function set tip(_arg1:ITip):void {
			this._tip = _arg1;
		}

		private function contrastBuff(_arg1:Object):void {
			var _local2:int = this._allBuffList.length;
			var _local3:int;
			while (_local3 < _local2) {
				if (_arg1.type == this._allBuffList[_local3]) {
					this.buffDataChange(_arg1, true);
					return;
				}
				;
				_local3++;
			}
			;
			this.addBuffRender(_arg1);
		}

		private function addBuffRender(_arg1:Object):void {
			this.addMc();
			_arg1.mc = this._buffMc;
			_arg1.doubleClick = this.doubleClick(_arg1.type);
			this._buffMc.addEventListener(MouseEvent.DOUBLE_CLICK, _arg1.doubleClick);
			var _local2:TextField = new TextField();
			_arg1.textField = _local2;
			var _local3:TextFormat = new TextFormat();
			_arg1.textFormat = _local3;
			this._allBuffData[_arg1.type] = _arg1;
			this._allBuffList.push(_arg1.type);
			this.buffDataChange(_arg1, false);
		}

		private function addMc():void {
			this._buffMc = new MovieClip();
			addChild(this._buffMc);
			this._buffMc.buttonMode = true;
			this._buffMc.visible = this._isShow;
		}

		private function buffDataChange(_arg1:Object, _arg2:Boolean):void {
			var _local4:String;
			var _local3:Object = this._allBuffData[_arg1.type];
			_local3.name = _arg1.name;
			_local3.effect = _arg1.effect;
			_local3.info = _arg1.info;
			_local3.mc.doubleClickEnabled = _arg1.doubleClickable;
			if (((!((_arg1.sign == _local3.sign))) || ((_arg2 == false)))) {
				_local4 = ((URI.buffIconUrl + _arg1.sign) + ".png");
				_local3.url = _local4;
				_local3.sign = _arg1.sign;
				this.loadBuffIcon(_local3);
			}
			;
			_local3.textField.htmlText = this.renderBuffInfo(_local3);
			_local3.textField.autoSize = TextFieldAutoSize.LEFT;
			_local3.textFormat.leading = 8;
			_local3.textField.setTextFormat(_local3.textFormat);
			if (_arg2 == false) {
				this._tip.addTarget(_local3.mc, _local3.textField);
			}
			;
			this.sortBuffList();
		}

		private function doubleClick(_arg1:String):Function {
			var type:* = _arg1;
			var func:* = function(_arg1:MouseEvent):void {
				clearBuff(type);
				_onDoubleRemoveBuff(type);
			};
			return (func);
		}

		private function loadBuffIcon(_arg1:Object):void {
			var icon:* = null;
			var data:* = _arg1;
			this.clearIcon(data.type);
			icon = new Image(data.url);
			data.icon = icon;
			icon.onComplete = function():void {
				icon.width = 24;
				icon.height = 24;
				data.mc.addChild(icon);
			};
			icon.mouseEnabled = false;
		}

		private function clearIcon(_arg1:String):void {
			var _local4:Object;
			var _local2:int = this._allBuffList.length;
			var _local3:int = (_local2 - 1);
			while (_local3 >= 0) {
				if (_arg1 == this._allBuffList[_local3]) {
					_local4 = this._allBuffData[_arg1];
					if (((!((_local4.icon == null))) && (_local4.icon.parent))) {
						_local4.icon.parent.removeChild(_local4.icon);
						_local4.icon = null;
						return;
					}
					;
				}
				;
				_local3--;
			}
			;
		}

		private function renderBuffInfo(_arg1:Object):String {
			var _local2:String = "";
			if (_arg1.name != "") {
				_local2 = (htmlFormat(_arg1.name, 14, 0xFFFFFF, false) + "\n");
			}
			;
			_local2 = (_local2 + (htmlFormat(_arg1.effect, 14, 65331, false) + htmlFormat(("\n" + _arg1.info), 12, 0xFFFFFF, false)));
			return (_local2);
		}

		public function htmlFormat(_arg1:String, _arg2:int = 12, _arg3:int = 0, _arg4:Boolean = false, _arg5:Boolean = false, _arg6:String = ""):String {
			return (HtmlText.format(_arg1, Math.max(_arg3, 0), Math.max(_arg2, 0), "", _arg4, false, _arg5, null, _arg6));
		}

		private function sortBuffList():void {
			var _local3:Object;
			var _local1:int = this._allBuffList.length;
			var _local2:int;
			while (_local2 < _local1) {
				_local3 = this._allBuffData[this._allBuffList[_local2]];
				_local3.mc.x = (91 + (30 * _local2));
				_local3.mc.y = 97;
				_local2++;
			}
			;
		}

		private function clearBuff(_arg1:String):void {
			var _local4:Object;
			var _local2:int = this._allBuffList.length;
			var _local3:int = (_local2 - 1);
			while (_local3 >= 0) {
				if (_arg1 == this._allBuffList[_local3]) {
					_local4 = this._allBuffData[_arg1];
					if (((!((_local4.icon == null))) && (_local4.icon.parent))) {
						_local4.icon.parent.removeChild(_local4.icon);
						_local4.icon = null;
					}
					;
					_local4.mc.removeEventListener(MouseEvent.DOUBLE_CLICK, _local4.doubleClick);
					removeChild(_local4.mc);
					delete this._allBuffData[_arg1];
					this._allBuffList.splice(_local3, 1);
					this.sortBuffList();
				}
				;
				_local3--;
			}
			;
		}

		public function clear():void {
			var _local3:String;
			var _local1:int = this._allBuffList.length;
			var _local2:int = (_local1 - 1);
			while (_local2 >= 0) {
				_local3 = this._allBuffList[_local2];
				this.clearBuff(_local3);
				_local2--;
			}
			;
		}

		public function set visibleBuff(_arg1:Boolean):void {
			var _local4:String;
			var _local5:Object;
			var _local2:int = this._allBuffList.length;
			var _local3:int = (_local2 - 1);
			while (_local3 >= 0) {
				_local4 = this._allBuffList[_local3];
				_local5 = this._allBuffData[_local4];
				_local5.mc.visible = _arg1;
				_local3--;
			}
			;
			this._isShow = _arg1;
		}

	}
}